package com.winshare.saleAssistant.service.impl;

import com.winshare.saleAssistant.dao.AreaMapper;
import com.winshare.saleAssistant.dao.RegionMapper;
import com.winshare.saleAssistant.domain.dto.req.BaseIdDTO;
import com.winshare.saleAssistant.domain.dto.req.RegionCreateDTO;
import com.winshare.saleAssistant.domain.dto.req.RegionDeleteDTO;
import com.winshare.saleAssistant.domain.dto.req.RegionUpdateDTO;
import com.winshare.saleAssistant.domain.dto.resp.RegionRespDTO;
import com.winshare.saleAssistant.domain.entity.AreaDO;
import com.winshare.saleAssistant.domain.entity.ProductInfoDO;
import com.winshare.saleAssistant.domain.entity.RegionAreaDO;
import com.winshare.saleAssistant.domain.entity.RegionDO;
import com.winshare.saleAssistant.exception.BusinessException;
import com.winshare.saleAssistant.mapper.RegionMapStruct;
import com.winshare.saleAssistant.service.RegionService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class RegionServiceImpl implements RegionService {

    private final RegionMapper regionMapper;
    private final AreaMapper areaMapper;

    public RegionServiceImpl(RegionMapper regionMapper, AreaMapper areaMapper) {
        this.regionMapper = regionMapper;
        this.areaMapper = areaMapper;
    }

    @Override
    public void saveRegion(RegionCreateDTO regionCreateDTO) {
        RegionDO regionDO = RegionMapStruct.INSTANCE.fromRegionCreateDTO(regionCreateDTO);
        if (checkRegionExist(regionDO)) {
            throw new BusinessException("当前大区已经存在");
        }
        regionMapper.saveRegion(regionDO);
        RegionDO regionDOInSql = regionMapper.getRegion(regionDO);
        List<AreaDO> areaDOList = regionDO.getAreaList();
        if (!CollectionUtils.isEmpty(regionDO.getAreaList())) {
            List<RegionAreaDO> regionAreaDOList = areaDOList.stream().map(item -> {
                RegionAreaDO regionAreaDO = new RegionAreaDO();
                regionAreaDO.setAreaId(item.getId());
                regionAreaDO.setRegionId(regionDOInSql.getId());
                return regionAreaDO;
            }).collect(Collectors.toList());
            regionMapper.batchInsertRegionAreaList(regionAreaDOList);
        }
    }

    @Override
    public void updateRegion(RegionUpdateDTO regionUpdateDTO) {
        RegionDO regionDO = RegionMapStruct.INSTANCE.fromRegionUpdateDTO(regionUpdateDTO);
        if (checkRegionExist(regionDO)) {
            throw new BusinessException("当前大区已存在");
        }
        regionMapper.updateRegion(regionDO);
        regionMapper.deleteRegionArea(regionDO);
        List<AreaDO> areaDOList = regionDO.getAreaList();
        for (AreaDO areaDO : areaDOList) {
            areaDO.setParentAreaName(regionUpdateDTO.getRegionName());
            areaMapper.updateArea(areaDO);
        }
        if (!CollectionUtils.isEmpty(regionDO.getAreaList())) {
            List<RegionAreaDO> regionAreaDOList = areaDOList.stream().map(item -> {
                RegionAreaDO regionAreaDO = new RegionAreaDO();
                regionAreaDO.setAreaId(item.getId());
                regionAreaDO.setRegionId(regionDO.getId());
                return regionAreaDO;
            }).collect(Collectors.toList());
            regionMapper.batchInsertRegionAreaList(regionAreaDOList);
        }
    }

    @Override
    public List<RegionRespDTO> getRegionList() {
        return regionMapper.getRegionList();
    }

    @Override
    public void deleteRegion(RegionDeleteDTO regionDeleteDTO) {
        RegionDO regionDO = RegionMapStruct.INSTANCE.fromRegionDeleteDTO(regionDeleteDTO);
        regionMapper.deleteRegion(regionDO);
        regionMapper.deleteRegionArea(regionDO);
    }

    @Override
    public RegionRespDTO getRegionDetail(BaseIdDTO baseIdDTO) {
        RegionDO regionDO = RegionMapStruct.INSTANCE.fromBaseIdDTO(baseIdDTO);
        RegionDO regionDOInSql = regionMapper.getRegion(regionDO);
        if (null == regionDOInSql) {
            throw new BusinessException("当前大区已经不存在");
        }
        RegionRespDTO regionRespDTO = RegionMapStruct.INSTANCE.fromRegionDO(regionDOInSql);
        return regionRespDTO;
    }

    private boolean checkRegionExist(RegionDO regionDO) {
        return regionMapper.countByRegion(regionDO) > 0;
    }
}
